<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://www.ccut.edu.cn/saturn" prefix="saturn"%>

<%@page import="java.util.*"%>
<%@page import="edu.ccut.saturn.component.*"%>

<%@ include file="/extjs/include/all.jsp"%>
<%@page import="edu.ccut.saturn.component.impl.mock.MockRequest"%>
<%@page import="org.json.JSONObject"%>
<%@page import="org.json.JSONException"%>
<html>
<head>
<title>值信息</title>
<style>   
   .x-grid3-cell-inner{white-space:normal;}   
</style>   
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/js/json.js"></script>
</head>
<body>
<%	
	Object value = null;
	if (request.getParameter("context") != null) {
		value = session.getAttribute(request.getParameter("context"));
		System.out.println("11111" + value);
	} else {
		String type = request.getParameter("type");
		String index = request.getParameter("index");
		
		if (index != null && !"".equals(index)) {
			List parameters = (List)session.getAttribute(type);
		
			if (parameters != null) {
				IParameter parameter = (IParameter)parameters.get(Integer.parseInt(index));
				value = parameter.getValue();
			}
			
		} else {
			value = type;
		}
	}
	
	if (value != null) {
		List valueList = null;
		String title = "值的内容";
		if (value instanceof List ) {
			valueList = (List)value;
			title = "List的内容";
		} else if (value instanceof Map) {
			valueList = new ArrayList(1);
			valueList.add(value);
			title = "Map的内容";
		} else if (value instanceof MockRequest) {
			valueList = new ArrayList();
			title = "Request的内容";
			HttpServletRequest httpServletRequest = (HttpServletRequest) value;
			
			Enumeration paraNames = httpServletRequest.getParameterNames();
			while (paraNames.hasMoreElements()) {
				String key = (String)paraNames.nextElement();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", key);
				oneValueMap.put("value", httpServletRequest.getParameter(key));
				oneValueMap.put("scope", "request.parameter");
				
				valueList.add(oneValueMap);
			}
			
			Enumeration names = httpServletRequest.getAttributeNames();
			while (names.hasMoreElements()) {
				String name = (String)names.nextElement();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", httpServletRequest.getAttribute(name));
				oneValueMap.put("scope", "request.attribute");
				
				valueList.add(oneValueMap);
			} 
			
			Enumeration sessionNames = httpServletRequest.getSession().getAttributeNames();
			while (sessionNames.hasMoreElements()) {
				String name = (String)sessionNames.nextElement();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", httpServletRequest.getSession().getAttribute(name));
				oneValueMap.put("scope", "session.attribute");
				
				valueList.add(oneValueMap);
			}
		} else if (value instanceof ILogicContext) {
		
			valueList = new ArrayList();
			title = "Context的内容";
			ILogicContext context = (ILogicContext)value;
			Iterator logicIterator = context.getValueKeys(IParameterInfo.SCOPE_LOGIC).iterator();
			while (logicIterator.hasNext()) {
				String name = (String)logicIterator.next();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", context.getValue(name, IParameterInfo.SCOPE_LOGIC));
				oneValueMap.put("scope", "logic");
				
				valueList.add(oneValueMap);
			}
			
			Iterator requestIterator = context.getValueKeys(IParameterInfo.SCOPE_REQUEST).iterator();
			if(requestIterator != null) {
			
			while (requestIterator.hasNext()) {
				String name = (String)requestIterator.next();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", context.getValue(name, IParameterInfo.SCOPE_REQUEST));
				oneValueMap.put("scope", "request");
				
				valueList.add(oneValueMap);
			}
			
			}
			
			Iterator sessionIterator = context.getValueKeys(IParameterInfo.SCOPE_SESSION).iterator();
			if(sessionIterator != null) {
			
			while (sessionIterator.hasNext()) {
				String name = (String)sessionIterator.next();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", context.getValue(name, IParameterInfo.SCOPE_SESSION));
				oneValueMap.put("scope", "session");
				
				valueList.add(oneValueMap);
			}
			
			}	
			
			Iterator applicatoinIterator = context.getValueKeys(IParameterInfo.SCOPE_APPLICATOIN).iterator();
			
			if(applicatoinIterator != null) {
			while (applicatoinIterator.hasNext()) {
				String name = (String)applicatoinIterator.next();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", context.getValue(name, IParameterInfo.SCOPE_APPLICATOIN));
				oneValueMap.put("scope", "application");
				
				valueList.add(oneValueMap);
			}
			}
		}
		
		if (valueList != null && !valueList.isEmpty()) {
			Object oneValue = valueList.get(0);
			request.setAttribute("showValue", valueList);
			request.setAttribute("title", title);
			%>
<%
	JSONObject json = new JSONObject();  
	System.out.println(valueList); 
	try {
		json.put("json", valueList);
	} catch (JSONException e) {
		e.printStackTrace();
	}
	String jsonStr = json.toString();
%>
<div id="result" style="width:100%"></div>
<script>
Ext.onReady(function(){
	var bodyWidth = Ext.get('result').getWidth();
	var json = <%=jsonStr%>;
	var store = new Ext.data.JsonStore({	
		fields:[
		<%		
			int columnCount = 0	;
			if (oneValue instanceof Map) {
				Map oneMap = (Map) oneValue;
				Set keySet = oneMap.keySet();

				Iterator iterator = keySet.iterator();
				
				while (iterator.hasNext()) {
					columnCount++;
					String key = (String)iterator.next();
		%>
					{name:'<%=key%>',mapping:'<%=key%>'}
		<%
					if(iterator.hasNext()){
						out.print(",");
					}
				}
			}
		%>
		],
		data:json.json
	});
	var cm = new Ext.grid.ColumnModel([ 
    	new Ext.grid.RowNumberer(),
        
        <%			
			if (oneValue instanceof Map) {
				Map oneMap = (Map) oneValue;
				Set keySet = oneMap.keySet();

				Iterator iterator = keySet.iterator();
				
				while (iterator.hasNext()) {
					String key = (String)iterator.next();
		%>
					{header:'<%=key%>',dataIndex:'<%=key%>',width:bodyWidth/<%=columnCount%>,renderer:function(value){
					//	alert(value);
					//	//return value.toJSONString();
						return value;
					}}
		<%
					if(iterator.hasNext()){
						out.print(",");
					}
				}
	 		}
		%>
 
    ]); 
	cm.defaultSortable = false; 
	var grid = new Ext.grid.GridPanel({ 
	    el: 'result', 
	    title:'<%=title%>',
	    frame:true,
	    autoHeight:true,
	    stripeRows: true,
	    trackMouseOver:false,
		enableColumnHide:false,
		enableHdMenu:false,
		enableColumnMove:false,
	    bodyStyle:'width:100%',
	    loadMask: {msg:'正在加载数据，请稍侯……'}, 
	    store:store, 
	    cm:cm
	}); 
    grid.render();
});
</script>
<%
		} else {
			out.print("无值信息");
		}
	} else {
	    out.print("无上下文信息");
	}
%>


</body>
</html>